package com.jpa.many.demo.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @ClassName DataSourceConfig
 * @description: TODO
 * @author: 朱同学
 * @Date 2021/4/12 22:10
 **/
@Configuration
public class DataSourceConfig {

    @Bean(name = "test1DataSource")
    @Qualifier("test1DataSource")
    @Primary //意思是在众多相同的bean中，优先使用用@Primary注解的bean.
    @ConfigurationProperties(prefix="spring.datasource.test1")
    public DataSource primaryDataSource() {

        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2DataSource")
    @Qualifier("test2DataSource")
    @ConfigurationProperties(prefix="spring.datasource.test2")
    public DataSource secondaryDataSource() {

        return DataSourceBuilder.create().build();
    }
}
